package com.tank.bean.item;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.DiscriminatorColumn;
import javax.persistence.DiscriminatorType;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import org.directwebremoting.annotations.DataTransferObject;
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.LazyCollection;
import org.hibernate.annotations.LazyCollectionOption;
import org.hibernate.annotations.LazyToOne;
import org.hibernate.annotations.LazyToOneOption;

import com.tank.bean.User;
@DataTransferObject
@Entity
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name="type",discriminatorType=DiscriminatorType.STRING)
@DiscriminatorValue("talk")
public class Item implements Serializable {
	
	@GenericGenerator(name = "gen", strategy = "increment")
	@Id
	@GeneratedValue(generator = "gen")
	private Integer id;//标识
	
	@ManyToOne(fetch=FetchType.EAGER)
	@LazyToOne(LazyToOneOption.FALSE)
	@Fetch(FetchMode.JOIN)
//	@Fetch(FetchMode.SUBSELECT)
	private User author;//作者
	
	private String title;//标题
	
	@Column(insertable=false,updatable=false)
	private String type;//类型
	
	private String content;//内容
	
	@Temporal(TemporalType.TIMESTAMP)
	private Date pubtime;//发布时间
	
	private Integer favourCount;//支持数
	
	private Integer commentCount;//评论数
	
	private Integer transpondCount;//转发数
	@OneToMany(cascade={CascadeType.ALL},mappedBy="item")
	private List<Image> images;//物品图片
	
	@OneToOne
	private Image cover;//封面图片
	
	private Boolean hide;//删除
	
	//private Item root;//转发根
	
	public String getType() {
		return type;
	}
	public void setType(String type) {
		this.type = type;
	}
	public Boolean getHide() {
		return hide;
	}
	public void setHide(Boolean hide) {
		this.hide = hide;
	}
	public List<Image> getImages() {
		return images;
	}
	public void setImages(List<Image> images) {
		this.images = images;
	}

	@ManyToOne
	private Item parent;//父元素
	
	@ManyToMany
	@JoinTable(name="at",
	joinColumns={@JoinColumn(name="iid",referencedColumnName="id")},
	inverseJoinColumns={@JoinColumn(name="uid",referencedColumnName="id")}
			)
	private List<User> atUsers=new ArrayList<User>(0);//我关注的人
	
	
	public Item() {
		this.setFavourCount(0);
		this.setCommentCount(0);
		this.setTranspondCount(0);
		this.setHide(false);
	}
	public Item(int id) {
		this.id=id;
		this.setFavourCount(0);
		this.setCommentCount(0);
		this.setTranspondCount(0);
		this.setHide(false);
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public User getAuthor() {
		return author;
	}

	public void setAuthor(User author) {
		this.author = author;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getContent() {
		
		return getFormatContent();
		//return content;
	}

	public void setContent(String content) {
		this.content = content;
	}
	public String getFormatContent() {
		
		if(content==null) return null;
		return content.replaceAll("\\[(\\d+),(\\w+)\\]","<a href=\"itemsProfileAction.action?userId=$1\">@$2</a>");
		//return null;
	}
	
	public Date getPubtime() {
		return pubtime;
	}

	public void setPubtime(Date pubtime) {
		this.pubtime = pubtime;
	}

	public Integer getFavourCount() {
		return favourCount;
	}

	public void setFavourCount(Integer favourCount) {
		this.favourCount = favourCount;
	}

	public Integer getCommentCount() {
		return commentCount;
	}

	public void setCommentCount(Integer commentCount) {
		this.commentCount = commentCount;
	}

	public Integer getTranspondCount() {
		return transpondCount;
	}

	public void setTranspondCount(Integer transpondCount) {
		this.transpondCount = transpondCount;
	}

	public Item getParent() {
		return parent;
	}

	public void setParent(Item parent) {
		this.parent = parent;
	}
	
	@Override
	public String toString() {
		
		System.out.println("=====");
		
		StringBuilder sb=new StringBuilder();
		sb.append("{");
		sb.append("作者：").append(this.getAuthor().getName()).append(",");
		sb.append("标题：").append(this.getTitle()).append(",");
		if(this.getParent() != null) sb.append("\n\t\t转发：").append(this.getParent()).append("\n,");
		sb.append("内容：").append(this.getContent()).append(",");
		sb.append("发布时间：").append(this.getPubtime()).append(",");
		sb.append("转发：").append(this.getTranspondCount()).append(",");
		sb.append("评论：").append(this.getCommentCount()).append(",");
		sb.append("}");
		return sb.toString();
	}
	public Image getCover() {
		return cover;
	}
	public void setCover(Image cover) {
		this.cover = cover;
	}
	public List<User> getAtUsers() {
		return atUsers;
	}
	public void setAtUsers(List<User> atUsers) {
		this.atUsers = atUsers;
	}
	
	
}
